<template>
  <div>
    <el-form :model="loginForm" :rules="rules" ref="loginForm">
    <el-form-item label="账号" prop="username">
      <el-input placeholder="请输入用户名" v-model="loginForm.account" prefix-icon="el-icon-user-solid" class="username"></el-input>
    </el-form-item>
      
    <el-form-item label="密码" prop="password">
      <el-input placeholder="请输入密码" v-model="loginForm.password" prefix-icon="el-icon-lock" show-password class="password"></el-input>
    </el-form-item>

    <!-- <el-form-item>
      <el-checkbox  label="记住登录状态" v-model="loginForm.isRemember" class="remember"></el-checkbox>
    </el-form-item> -->

    <el-form-item>
      <el-button @click="login">登录</el-button>
    </el-form-item>
  </el-form>
  </div>
</template> 

<script>

export default {
    name:'LoginForm',
    data(){
      return{
          loginForm:{
            account: "",
            password: ""
          },
          isRemember:false,
          rules: {
            account: [
              { required: true, message: '请输入账号', trigger: 'blur' },
              { pattern: /^[a-zA-Z0-9]{1,12}$/, message: '账号格式不正确', trigger: 'blur' }
            ],
            password: [
              { required: true, message: '请输入密码', trigger: 'blur' },
              { pattern: /^[a-zA-Z0-9]{1,12}$/, message: '密码格式不正确', trigger: 'blur' }
            ]
          }
        }
    },   
    methods:{
      login(){
        this.$request.post("/admin/login",this.loginForm)
        .then(res=>{
          if(res.data == null){
            this.$message({
              message: res.msg,
              type: 'warning'
          })
          }else{
            console.log("用户："+res.data)
            localStorage.setItem("admin",res.data)
            // console.log(localStorage.getItem("admin"))
            this.$router.push("/")
          }
        })
      }
    }
  }
</script>

<style scoped>
  button{
    display: block;
    width: 100%;
    background-color: #e32626;
    color: white;
    margin-top: 40px;
  }

  button:hover,button:active{
    background-color: #d85b5b;
    color: white;
  }

  .username,.password{
    margin-top: 30px;
  }

  .remember{
    margin-right: 100%;
    margin-top: 10px;
    text-align: left;
  }

</style>